from django.http import HttpResponse
import pyfo
from django.shortcuts import render_to_response
from simdb.models import Experiment
from simdb.models import Snapshot
# TODO: Add an experiment.type column
# TODO MAYBE: Query listexperiments & experimenttype?
#  , Simulation, PostProcessing \
#    Visualisation, ClusterDetection, SubvolumeExtraction

def get_params(request):
    items = []
    for param, val in request.GET.iteritems():
        listitem = param
        if val:
            listitem += ' = ' + val
        items.append(('li', listitem))
    if 'goo' in request.GET:
        items.append(('li', 'goo = True'))
    if 'GOO' in request.GET:
        items.append(('li', 'GOO = True'))
    if 'Goo' in request.GET:
        items.append(('li', 'Goo = True'))
    for k in request.GET.keys():
        items.append(('li', 'key = ' + k))
    a = request.GET.get('foo')
    items.append(('li', 'a = ' + str(a)))

    data = dict((k.lower(), v) for k, v in request.GET.items())
    if 'goo' in data:
        items.append(('li', 'goo in data = True'))
    parameters = list(k.lower() for k in request.GET.keys())
    if 'moo' in parameters:
        items.append(('li', 'moo in parameters = True'))

    html = pyfo.pyfo(('html',('body',('ul',items))))
    return HttpResponse(html, mimetype='text/html')

def get(request):
    # convert the keys to lowercase 
    parameters = dict((k.lower(), v) for k, v in request.GET.iteritems())

    if 'listexperiments' in parameters:
        return list_experiments(request)
    elif 'listsnapshots' in parameters:
        return list_snapshots(request, parameters)

    a = {'b':'c'}
    return render_to_response('simdap/vot.xml', {'a': a},
                              mimetype='text/xml')

def list_experiments(request):
    return render_to_response('simdap/experiments.xml',                         
                              {'experiments': Experiment.objects.all()},
                              mimetype='text/xml')

def list_snapshots(request, parameters):
    if 'experiment' in parameters:
        experiment = parameters['experiment']
        e = {'experiments': list(Experiment.objects.filter(
                    publisherDID__exact=experiment))}
    else:
        e = {'experiments': Experiment.objects.all()}

    return render_to_response('simdap/snapshots.xml', e,
                              mimetype='text/xml')


def html(request):
    return render_to_response('simdap/cadac.html')

def html_data(request):
    return render_to_response('simdap/cadac_data.html',                         
                              {'experiments': Experiment.objects.all()})
